import 'package:flutter_deer/database/database_manager.dart';
import 'package:objectbox/objectbox.dart';

import 'package:flutter_deer/dl/dl.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/pay_type_model.g.dart';
import 'package:flutter_deer/res/emuns/pay_type_enum.dart';

@Entity()
@JsonSerializable()
class PayTypeModel {
  @Id() // 自增主键
  int id = 0;

  bool switchConsumePay = true;
  bool switchPrint = true;
  int mSortNo = 0;//本地的
  int sortNo = 0;
  String paymentName = '';
  String paymentScene = '';
  String paymentCode = '';
  String paymentModeId = '';
  int paymentType = 0;
  String? merchantPaymentModeId = '';
  String payMethod = '';

  @Transient()
  bool choose = false;

  PayTypeModel();

  factory PayTypeModel.fromJson(Map<String, dynamic> json) => $PayTypeModelFromJson(json);

  Map<String, dynamic> toJson() => $PayTypeModelToJson(this);

  // 获取支付方式
  String getPayTypeDiscount() {
    if (paymentCode == PayTypeEnum.Custom.paymentCode) {
      return paymentModeId;
    }
    return paymentCode;
  }

  // 判断是否是移动支付
  bool isMobilePay() {
    return paymentCode == PayTypeEnum.MobilPay.paymentCode;
  }

  // 获取支付方式
  String getOrderPayType() {
    if (paymentCode == PayTypeEnum.Custom.paymentCode) {
      return paymentModeId;
    }
    if (paymentCode == PayTypeEnum.MobilPay.paymentCode) {
      return payMethod;
    }
    return paymentCode;
  }

  @override
  String toString() {
    return 'PayTypeModel{id:$id Pay: $switchConsumePay, [$paymentName] mSortNo: $mSortNo, paymentModeId: $paymentModeId,}';
  }

  // 深度拷贝
  PayTypeModel copyWith() {
    return PayTypeModel()
      ..id = id
      ..switchConsumePay = switchConsumePay
      ..switchPrint = switchPrint
      ..mSortNo = mSortNo
      ..sortNo = sortNo
      ..paymentName = paymentName
      ..paymentScene = paymentScene
      ..paymentCode = paymentCode
      ..paymentModeId = paymentModeId
      ..paymentType = paymentType
      ..merchantPaymentModeId = merchantPaymentModeId
      ..payMethod = payMethod
      ..choose = choose;
  }

  Future<void> deleteAll() async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<PayTypeModel>();
    box.removeAll();
  }

  // 保存到数据库
  static Future<void> saveAll(List<PayTypeModel> list) async {
    // 保存到数据库
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<PayTypeModel>();
    final i = box.removeAll() ;
    print('----> box.removeAll $i');
    list.sort((a, b) => a.mSortNo.compareTo(b.mSortNo));
    for (int i = 0; i < list.length; i++) {
      print(list[i]);
    }
    box.putMany(list);
  }

  // 获取所有
  static Future<List<PayTypeModel>> getAll() async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<PayTypeModel>();
    final all = box.getAll();
    all.sort((a, b) => a.mSortNo.compareTo(b.mSortNo));
    return all;
  }

  // 更新到数据库
  static Future<void> updateAll(List<PayTypeModel> netList) async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<PayTypeModel>();
    final localList = box.getAll();
    if (localList.isEmpty) {
      box.putMany(netList);
    } else {
      box.removeAll();
      final resultList = List<PayTypeModel>.empty(growable: true);
      for (final netItem in netList) {
        for (final localItem in localList) {
          if (netItem.payMethod == localItem.payMethod) {
            netItem.switchPrint = localItem.switchPrint;
            netItem.switchConsumePay = localItem.switchConsumePay;
            // netItem.sortNo = localItem.sortNo;
            netItem.mSortNo = localItem.mSortNo;
            resultList.add(netItem);
            print('更新到数据库 $netItem');
          }
        }
      }

      // 如果没有混合支付
      if (!resultList.any((element) => element.paymentCode == PayTypeEnum.MIX.paymentCode)) {
        resultList.add(PayTypeModel()
          ..paymentCode = PayTypeEnum.MIX.paymentCode
          ..paymentName = PayTypeEnum.MIX.title
          ..paymentScene = '[1]'
          ..mSortNo = 100
          ..switchConsumePay = true);
      }

      // 删除刷脸支付和刷银行卡支付
      resultList.removeWhere((element) => element.paymentCode == PayTypeEnum.FACE.paymentCode || element.paymentCode == PayTypeEnum.Bank.paymentCode);

      // 保存
      box.putMany(resultList);
    }
  }
}
